home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / ups / apcd-0.5 / apcd-0 / apcd.h < prev    next >
C/C++ Source or Header  |  1995-11-07  |  8KB  |  287 lines

  1. /* apcd.h - Definitons file for APC SmartUPS daemon
  2.  *
  3.  * Copyright (c) 1995 Pavel Korensky
  4.  * All rights reserved
  5.  *
  6.  * Version:
  7.  * 
  8.  * $Id: apcd.h,v 1.5 1995/11/07 12:40:24 root Exp root $
  9.  *
  10.  *
  11.  * History:
  12.  *
  13.  * $Log: apcd.h,v $
  14.  * Revision 1.5  1995/11/07  12:40:24  root
  15.  * Version 0.5 Beta uploaded to the sunsite
  16.  *
  17.  * Revision 1.4  1995/05/23  01:08:18  root
  18.  * Parameters are on the command line, instead of the config.h file
  19.  *
  20.  * Revision 1.3  1995/05/23  00:26:08  root
  21.  * UPS switch-off was added
  22.  *
  23.  * Revision 1.2  1995/05/21  20:15:56  root
  24.  * First ALPHA version
  25.  *
  26.  * Revision 1.1  1995/05/20  12:22:54  root
  27.  * Initial revision
  28.  *
  29.  *
  30.  */
  31.  
  32. /* 
  33.  * A few words regarding the APC serial protocol
  34.  * 
  35.  * Firstly, the cable:
  36.  *
  37.  * You will need a simple 3 wires cable connected as follows:
  38.  *
  39.  * PC (9 pin)     APC
  40.  *  2 RxD          2
  41.  *  3 TxD          1
  42.  *  5 GND          9
  43.  *
  44.  * Communication protocol which is used for controlling the APC SmartUPS
  45.  * is very simple ASCII protocol.
  46.  * The serial communication is 2400 Bd, 8N1
  47.  *
  48.  * Basically, your computer send a character to the UPS and UPS answer
  49.  * with the requested information in the form of the ASCII string terminated
  50.  * with CR/LF.
  51.  * In some cases, the UPS answer is the action like battery test etc.
  52.  *
  53.  * There are two exceptions. If the UPS is switching to the battery, the
  54.  * characters "!!" are sent to the computer. If the UPS is switching back
  55.  * on-line, the charater '$' is sent to the computer. If battery is low,
  56.  * '%' is sent to the computer. When battery is recharged to more than 15%,
  57.  * '+' is sent to the computer.
  58.  *
  59.  * The protocol description is not based on informations obtained from APC.
  60.  * I tried to analyse the protocol on RS-232 port, so my description
  61.  * is not complete.
  62.  *
  63.  * Command are:
  64.  *
  65.  *    SEND            ANSWER
  66.  *
  67.  *    f            Battery status in %   100 = fully loaded
  68.  *    N            Line minimum voltage (V)
  69.  *    M            Line maximum voltage (V)
  70.  *    Q            Status byte 8 bits long.
  71.  *                              - 08  On-line LED ON
  72.  *                              - 10  On Battery LED ON
  73.  *                              - 18  Both LEDs ON
  74.  *                              - 02  UPS in shut down state, waiting for AC                  
  75.  *
  76.  *                              0 - If 1, UPS is in calibration
  77.  *                              1 - If 1, UPS is sleeping
  78.  *                              2 -  
  79.  *                              3 - If 1, AC is here
  80.  *                              4 - If 1, UPS is running on battery
  81.  *                              5 -
  82.  *                              6 - If 1, Battery is low
  83.  *                              7 - 
  84.  *
  85.  *
  86.  *    P            UPS load (VA)
  87.  *    F            Line frequency (Hz)
  88.  *    L            Line voltage (V)
  89.  *    O            Output voltage (V)
  90.  *    C            Inside temperature (Celsius degrees)
  91.  *    B            Battery voltage
  92.  *
  93.  *
  94.  * Some signals for UPS control
  95.  *
  96.  *      Y                       Attention
  97.  *    W            Switch on battery
  98.  *    X            Switch back on-line
  99.  *    A            Lights test
  100.  *    U            Power failure test
  101.  *    c            UPS identification ???
  102.  *
  103.  * One very special signal
  104.  *
  105.  *    Z ~1500ms pause Z    Switch off the UPS completely and imediately
  106.  *
  107.  *
  108.  * Special thanks to Kevin D. Smolkowski (kevins@tigger.oslc.org)
  109.  *  who observed other control codes
  110.  *
  111.  *      D                       Runtime calibration   ON/OFF
  112.  *      V                       Firmware revison
  113.  *      c                       UPS Identification
  114.  *      k                       UPS Warning (Power Fail, Power Fail + 30, Low Battery, OFF      
  115.  *      l                       Low transfer point
  116.  *      m                       Manufacture date
  117.  *      n                       UPS serial number
  118.  *      o                       Nominal UPS output
  119.  *      p                       UPS turn off delay "020" "180" "300" "600" Cycles
  120.  *      q                       Low battery signal time "02" "05" "07" "10" Cycles
  121.  *      r                       Wakeup Delay
  122.  *      s                       Sensitivity "H" "M" "L"
  123.  *      u                       High transfer point
  124.  *      x                       Battery replacement date
  125.  *      y                       Brand "(C) APCC"
  126.  *      7                       Option switch settings (Hex) "0F" "0E" "0C" "08" "00"
  127.  *      
  128.  *      -                       Change prev. settings
  129.  *
  130.  *                              To change UPS name   c - /n newname /n
  131.  *
  132.  *
  133.  * Further codes, not fully tested
  134.  *
  135.  *      S                       Soft shutdown, UPS will go back on-line when AC is back
  136.  *      @222                    Soft shutdown, UPS will NOT go back when AC is back
  137.  *      U                       Power failure test
  138.  *      G                       Return 'T' if there was a power failure 
  139.  *                              Return 'S' if there was not a power failure
  140.  *                              UPS Test will reset the value to the 'S'
  141.  *      K 1500ms pause K        Switch off UPS completely after delay
  142.  *
  143.  */
  144.  
  145.  
  146. #include <stdio.h>
  147. #include <stdlib.h>
  148. #include <stddef.h>
  149. #include <unistd.h>
  150. #include <utmp.h>
  151. #include <paths.h>
  152. #include <string.h>
  153. #include <dirent.h>
  154. #include <fcntl.h>
  155. #include <ctype.h>
  156. #include <errno.h>
  157. #include <syslog.h>
  158. #include <time.h>
  159. #include <mntent.h>
  160. #include <sys/signal.h>
  161. #include <sys/param.h>
  162. #include <sys/mount.h>
  163. #include <sys/time.h>
  164. #include <sys/wait.h>
  165. #include <sys/types.h>
  166. #include <sys/stat.h>
  167. #include <sys/socket.h>
  168. #include <sys/ioctl.h>
  169. #include <sys/termios.h>
  170. #include <sys/resource.h>
  171. #include <netinet/in.h>
  172. #include <arpa/inet.h>
  173. #include <netdb.h>
  174.  
  175. /*
  176.  * Various constants
  177.  */
  178.  
  179. #define    DEBUGGING 1    /* Debug flag */
  180.  
  181. #define    CONFIGFILENAME    "/etc/apcd.conf"
  182. #define UPSLOGFILENAME  "/var/adm/upsstat.log"    /* The name of the logfile with UPS status */
  183. #define    MAXLINE 100    /* Max. length of the UPS anser */
  184. #define PAUSETIME 1    /* how many seconds should apcd sleep each time
  185.                it checks the UPS status
  186.              */
  187. #define BATT 1        /* UPS switched on battery during reading info */
  188. #define LINE 0        /* UPS switched back on-line during reading info */
  189.  
  190. #define MAX_SLAVES    10
  191. #define UPS_TCP_PORT    6666    /* Our communication port */
  192.              
  193. #define DEFAULT_SPEED B2400
  194.  
  195. #define    BATT_FULL    'f'
  196. #define    UPS_LINE_MIN    'N'
  197. #define UPS_LINE_MAX    'M'
  198. #define    UPS_STATUS    'Q'
  199. #define    UPS_LOAD    'P'
  200. #define    LINE_FREQ    'F'
  201. #define    LINE_VOLTAGE    'L'
  202. #define    OUTPUT_VOLTAGE    'O'
  203. #define    UPS_TEMP    'C'
  204. #define    BATT_VOLTAGE    'B'
  205.  
  206. #define    GO_ON_BATT    'W'
  207. #define    GO_ON_LINE    'X'
  208. #define    LIGHTS_TEST    'A'
  209. #define    FAILURE_TEST    'U'
  210.  
  211. #define    UPS_ON_BATT    '!'
  212. #define    UPS_ON_LINE    '$'
  213. #define BATT_LOW        '%'
  214. #define BATT_OK         '+'
  215.  
  216. #define _PATH_UMOUNT    "/bin/umount"
  217. #define UMOUNT_ARGS    "umount","-a"
  218. #define _PATH_MTAB    "/etc/mtab"
  219.  
  220. /* Several typedefs */
  221.  
  222. typedef    unsigned char BYTE;
  223. typedef unsigned short int  WORD;
  224.  
  225. typedef    struct {
  226.     double    BatLoad;
  227.     double    LineMin;
  228.     double    LineMax;
  229.     double    UPSLoad;
  230.     double    LineFreq;
  231.     double    LineVoltage;
  232.     double    OutputVoltage;
  233.     double    UPSTemp;
  234.     double    BattVoltage;
  235.     int    Status;
  236. } UPSINFO;
  237.  
  238.  
  239. /* Macros */
  240.  
  241. /* Variables */
  242. extern int    slave;
  243. extern char    *use_port;
  244. extern char    *master_name;
  245. extern char    *logfilename;
  246. extern char    *slaves[MAX_SLAVES];
  247. extern int    num_slaves;
  248. extern int    power_timer;
  249. extern int    log_timer;
  250. extern int    gottimeout;
  251. extern int    mastertimeout;
  252. extern int    gotpowerok;
  253. extern int    masterbatlow;
  254. extern int    socketfd;
  255. extern int    newsocketfd;
  256. extern struct sockaddr_in slave_adr[MAX_SLAVES];
  257. extern int    slavesocket[MAX_SLAVES];
  258.  
  259. /* Function prototypes */
  260.  
  261.  
  262. void start_daemon(void);
  263. void signal_setup(void);
  264. void sig_intr(int);
  265. void sig_term(int);
  266. void dump_status (int);
  267. void go_down(void);
  268. void go_down_batt(void);
  269. void send_second_z(int);
  270. void do_shutdown(void);
  271. void write_wtmp(void);
  272. void umount_disks(void);
  273. void umount_disks_ourselves(void);
  274.  
  275. void setup_tty(void);
  276. int getline(int,char *);
  277. int fillUPS(int,UPSINFO *);
  278. void mesusr(char *,struct utmp *);
  279. void mesall(char *);
  280. int parse_config(void);
  281. void log_UPS_status(void);
  282. int get_master_message(int);
  283. int send_to_slaves(int);
  284. int send_info(int,int);
  285. int prepare_slave(void);
  286. int prepare_master(void);
  287.